package org.example;
import java.util.Random;
import java.util.Scanner;
public class Lab_01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();
        int totalQuestions = 50;
        int correctAnswers = 0;

        System.out.println("欢迎来到100以内加减法口算练习！");
        System.out.println("你将回答" + totalQuestions + "道题目。\n");

        for (int i = 1; i <= totalQuestions; i++) {
            // 随机决定加法还是减法
            boolean isAddition = random.nextBoolean();
            int num1, num2, answer;
            String operator;

            if (isAddition) {
                // 生成加法题，确保和不超过100
                num1 = random.nextInt(50) + 1; // 1-50
                num2 = random.nextInt(50) + 1; // 1-50
                answer = num1 + num2;
                operator = "+";
            } else {
                // 生成减法题，确保结果非负
                num1 = random.nextInt(100) + 1; // 1-100
                num2 = random.nextInt(num1) + 1; // 1-num1
                answer = num1 - num2;
                operator = "-";
            }

            System.out.print("第" + i + "题: " + num1 + " " + operator + " " + num2 + " = ");
            int userAnswer = scanner.nextInt();

            if (userAnswer == answer) {
                System.out.println("正确！\n");
                correctAnswers++;
            } else {
                System.out.println("错误！正确答案是: " + answer + "\n");
            }
        }

        System.out.println("练习结束！你的得分是: " + correctAnswers + "/" + totalQuestions);
        double percentage = (double)correctAnswers / totalQuestions * 100;
        System.out.printf("正确率: %.1f%%\n", percentage);

        scanner.close();
    }
}
